﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;
using System.IO;

namespace RMS
{
    public partial class frmShopInfo : ICommonForm
    {
        public frmShopInfo()
        {
            InitializeComponent();

            DisplayInfo();
        }

        private void DisplayInfo()
        {
            ShopInfoManager mng = new ShopInfoManager();
            var info = mng.GetOne();

            if (info != null)
            {
                txtShopName.Text = info.ShopName;
                txtAddress.Text = info.Address;
                txtPhone.Text = info.Phone;
                txtFax.Text = info.Fax;
                txtEmail.Text = info.Email;
                txtWebsite.Text = info.Website;
                txtLanguage.Text = info.Language;
                txtCurrency.Text = info.Currency;
                txtDecimalPlaces.Value = info.DecimalPlaces;
                txtLogoPath.Text = info.Logo;
                pictureBox1.ImageLocation = String.Format("{0}\\Images\\{1}", Environment.CurrentDirectory, info.Logo);
            }
        }

        private void txtDecimalPlaces_ValueChanged(object sender, EventArgs e)
        {
            decimal sampleNumber = 1000000.4567M;

            lblNumberFormatSample.Text = Utilities.FormatNumber(sampleNumber, Convert.ToInt32(txtDecimalPlaces.Value));

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ShopInfo info = new ShopInfo();
            info.Address = txtAddress.Text;
            info.Currency = txtCurrency.Text;
            info.Email = txtEmail.Text;
            info.Fax = txtFax.Text;
            info.Language = txtLanguage.Text;
            info.Phone = txtPhone.Text;
            info.Website = txtWebsite.Text;
            info.DecimalPlaces = Convert.ToInt32(txtDecimalPlaces.Value);
            info.ShopName = txtShopName.Text;
            info.Logo = txtLogoPath.Text;
            

            ShopInfoManager mng = new ShopInfoManager();
            mng.Update(info);

            MessageBox.Show("Đã lưu thông tin");
        }

        private void btnChangeLogo_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
                File.Copy(openFileDialog1.FileName, String.Format("{0}\\Images\\{1}", Environment.CurrentDirectory, openFileDialog1.SafeFileName));
                txtLogoPath.Text = openFileDialog1.SafeFileName;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
     
    }
}
